package m4d1;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

//tcp客户端，向服务器发数据，接受服务器数据
//Socket该类实现客服端套接字，套接字包含IP和端口号
public class cTCPClient {
    public static void main(String[] args) throws IOException {
        //构造绑定服务器IP地址和端口号
        Socket socket = new Socket("127.0.0.1",8888);

        //获取网络字节输出流对象
        OutputStream os = socket.getOutputStream();

        //给服务器发数据
        os.write("服务器好啊".getBytes());

        //获取网络字节输入流对象
        InputStream is = socket.getInputStream();

        //读取客户端回写的数据
        byte[] bytes = new byte[1024];
        int len = 0;
        len = is.read(bytes);
        System.out.println(new String(bytes,0,len));

        socket.close();
    }
}
